suggestion substring and more

suggestion substring and more

am 09.10.2008 20:45:59 von Mario Sanchez

dear perl gurus

can someone suggest a design/approach on solving the following requirement:

there is a string, say $ss, that is contained in a longer string, say $ls.
for example $ss = "jumps over" and $ls = "the cow jumps over the moon"

how do you suggest obtaining a string from $ls that is $n characters to
the both left and right of $ss?

for example and using the above, say $n = 4
so the results would be "cow jumps ove" (notice 4 characters to both the
left and right of the target text $ss.

any approach would be much appreciated!

mario sanchez

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: suggestion substring and more

am 09.10.2008 21:13:24 von Christian Walde

On Thu, 09 Oct 2008 20:45:59 +0200, Mario R. Sanchez, Ph.D. u.edu> wrote:

> can someone suggest a design/approach on solving the following requiremen=
t:
>
> there is a string, say $ss, that is contained in a longer string, say $ls.
> for example $ss =3D "jumps" and $ls =3D "the cow jumps over the moon"
>
> how do you suggest obtaining a string from $ls that is $n characters to
> the both left and right of $ss?
>
> for example and using the above, say $n =3D 4
> so the results would be "cow jumps ove" (notice 4 characters to both the
> left and right of the target text $ss.

You could do that with a relatively simple regular expression.

--

#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;

my $longstring =3D "the cow jumps over the moon";
my $shortstring =3D "jumps";

if ( $longstring =3D~ m{(....$shortstring....)} ) {
my $result =3D $1;
say $1;
}

--

You may also want to review a reference for the syntax here: http://www.gre=
enend.org.uk/rjk/2002/06/regexp.html

A more complex solution would be:

--

#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;

my $n =3D 4;
my $longstring =3D "the cow jumps over the moon";
my $shortstring =3D "jumps";
my $regexp =3D ('.' x $n) . $shortstring . ('.' x $n);

if ( $longstring =3D~ m{($regexp)} ) {
my $result =3D $1;
say $1;
}

-- =

Grüsse,
Christian Walde

As too many people send me top- and fully-quoted
emails, here is a guide on how to be considerate
when sending emails to other people:
http://www.xs4all.nl/~hanb/documents/quotingguide.html
--

=

___________________________________________________________ =

Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: suggestion substring and more

am 09.10.2008 21:23:07 von Williamawalters

--===============0297139443==
Content-Type: multipart/alternative;
boundary="-----------------------------1223580187"


-------------------------------1223580187
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit


hi mario --

In a message dated 10/9/2008 1:46:26 P.M. Eastern Standard Time,
msanc010@cs.fiu.edu writes:

> dear perl gurus
>
> can someone suggest a design/approach on solving the following
> requirement:
>
> there is a string, say $ss, that is contained in a longer string, say $ls.
> for example $ss = "jumps over" and $ls = "the cow jumps over the moon"
>
> how do you suggest obtaining a string from $ls that is $n characters
> to the both left and right of $ss?
>
> using above, say $n = 4 so results would be "cow jumps ove" (notice 4
> characters to both the left and right of the target text $ss.
>
> any approach would be much appreciated!
>
> mario sanchez


first, i assume there has been some corruption of your original e-mail
and "cow jumps ove" should read "cow jumps over the".

try something along the lines of:

perl -wMstrict -le
"my $ls = 'the cow jumps over the';
my $ss = 'jumps over';
my $n = 4;
my $regex = qr{ .{$n} \Q$ss\E .{$n} }xms;
print q{``}, $ls =~ /($regex)/, q{''};
"
``cow jumps over the''

perl -wMstrict -le
"my $ls = 'the cow jumps xxx over the';
my $ss = 'jumps over';
my $n = 4;
my $regex = qr{ .{$n} \Q$ss\E .{$n} }xms;
print q{``}, $ls =~ /($regex)/, q{''};
"
``''

perl -wMstrict -le
"my $ls = 'the cow jumps over the';
my $ss = 'jumps xxx over';
my $n = 4;
my $regex = qr{ .{$n} \Q$ss\E .{$n} }xms;
print q{``}, $ls =~ /($regex)/, q{''};
"
``''

see also the standard man pages perlre, perlretut and perlrequick
(e.g., ``perldoc perlre'').

hth -- bill walters


**************New MapQuest Local shows what's happening at your destination.
Dining, Movies, Events, News & more. Try it out
(http://local.mapquest.com/?ncid=emlcntnew00000002)

-------------------------------1223580187
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable





Arial"=20
bottomMargin=3D7 leftMargin=3D7 topMargin=3D7 rightMargin=3D7> e_document=20
face=3DArial color=3D#000000 size=3D2>


hi mario --  

 

In a message dated 10/9/2008 1:46:26 P.M. Eastern Standard Time,=20
msanc010@cs.fiu.edu writes:

 

> dear perl gurus
>
> can someone suggest a design/appr=
oach=20
on solving the following
> requirement:
>
> there is a=20
string, say $ss, that is contained in a longer string, say $ls.
> for=20
example $ss =3D "jumps over" and $ls =3D "the cow jumps over the moon"
&g=
t;=20

> how do you suggest obtaining a string from $ls that is $n=20
characters
> to the both left and right of $ss?
>
> using=
=20
above, say $n =3D 4 so results would be "cow jumps ove" (notice 4
>=20
characters to both the left and right of the target text $ss.
>
&g=
t;=20
any approach would be much appreciated!
>
> mario sanchez
DIV>
 

first, i assume there has been some corruption of your original e-mail=20

and "cow jumps ove" should read "cow jumps over the".   >
 

try something along the lines of:  

 

perl -wMstrict -le
"my $ls =3D 'the cow jumps over the';
 my=
$ss =
'jumps over';
 my $n  =3D 4;
 my $regex =3D qr{ .{$n} \=
Q$ss\E=20
..{$n} }xms;
 print q{``}, $ls =3D~ /($regex)/, q{''};
"
``cow=20=
jumps=20
over the''

 

perl -wMstrict -le
"my $ls =3D 'the cow jumps xxx over the';
&nbs=
p;my=20
$ss =3D 'jumps over';
 my $n  =3D 4;
 my $regex =3D qr{=
.{$n}=20
\Q$ss\E .{$n} }xms;
 print q{``}, $ls =3D~ /($regex)/,=20
q{''};
"
``''

 

perl -wMstrict -le
"my $ls =3D 'the cow jumps over the';
 my=
$ss =
'jumps xxx over';
 my $n  =3D 4;
 my $regex =3D qr{ .{$=
n}=20
\Q$ss\E .{$n} }xms;
 print q{``}, $ls =3D~ /($regex)/,=20
q{''};
"
``''

 

see also the standard man pages perlre, perlretut and perlrequick >
(e.g., ``perldoc perlre'').  

 

hth -- bill walters  

 



=3D"5e6259ca8de2b77e267dbe98ee2ffd"> l 10pt ARIAL, SAN-SERIF;">
New MapQuest Loc=
al
shows what's happening at your destination. Dining, Movies, Events, N=
ews & more. =
Try it out!


-------------------------------1223580187--

--===============0297139443==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0297139443==--

RE: suggestion substring and more

am 09.10.2008 21:29:19 von HBullock

there is a string, say $ss, that is contained in a longer string, say $ls.
for example $ss = "jumps over" and $ls = "the cow jumps over the moon"

how do you suggest obtaining a string from $ls that is $n characters to
the both left and right of $ss?

for example and using the above, say $n = 4
so the results would be "cow jumps ove" (notice 4 characters to both the
left and right of the target text $ss.
---------------------

use strict;
my $ss = "jumps";
my $ls = "the cow jumps over the moon";
my $n = 4;

$ls =~ /$ss/;
print "'". substr($`,0-$n) . $& . substr($', 0, $n) . "'";

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: suggestion substring and more

am 09.10.2008 21:41:02 von Christian Walde

On Thu, 09 Oct 2008 21:29:19 +0200, Bullock, Howard A. onics.com> wrote:

> print "'". substr($`,0-$n) . $& . substr($', 0, $n) . "'";

You should really
use English;
if you're gonna do it that way. ;)

http://perldoc.perl.org/perlvar.html

-- =

Grüsse,
Christian Walde

As too many people send me top- and fully-quoted
emails, here is a guide on how to be considerate
when sending emails to other people:
http://www.xs4all.nl/~hanb/documents/quotingguide.html
--

=

___________________________________________________________ =

Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: suggestion substring and more

am 10.10.2008 13:31:17 von Brian Raven

Mario R. Sanchez, Ph.D. <> wrote:
> dear perl gurus
>
> can someone suggest a design/approach on solving the following
> requirement:
>
> there is a string, say $ss, that is contained in a longer string, say
> $ls.
> for example $ss = "jumps over" and $ls = "the cow jumps over the moon"
>
> how do you suggest obtaining a string from $ls that is $n characters
> to the both left and right of $ss?
>
> for example and using the above, say $n = 4 so the results would be
> "cow jumps ove" (notice 4 characters to both the left and right of
> the target text $ss.

Surely, that should be "cow jumps over the", i.e. including the 4
characters either side of "jumps over".

>
> any approach would be much appreciated!

Well, its easy to find out the offset of the substring (see 'perldoc -f
index'), and you know how long the substring is (see 'perldoc -f
length'), so its just some simple arithmetic and a call to substr (see
'perldoc -f substr') to extract what you want.

HTH

--
Brian Raven

------------------------------------------------------------ -----------------------------------------------
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs